const { Buffer } = require("buffer");
class OnePxGIF {
  constructor() {}
  generateBase64(color) {
    let buffer = this.generateBuffer(color);
    return "data:image/gif;base64," + buffer.toString("base64");
  }
  _toColorArray(color){
    if(color && (color.length == 6 || color.length == 7)){
      if(color.length == 7){
        color = color.replace('#', '');
      }
      let colorArray = [];
      colorArray.push(parseInt('0x' + color.slice(0, 2)))
      colorArray.push(parseInt('0x' + color.slice(2, 4)))
      colorArray.push(parseInt('0x' + color.slice(4, 6)))
      return colorArray;
    }
  }
  generateBuffer(color) {
    let buffer = Buffer.alloc(50);
    let index = 0;
    let colorArray = this._toColorArray(color);
    let isTransparen = !colorArray || colorArray.length == 0; // 是否透明

    index = this._appendHeader(buffer, index); // 添加头部
    index = this._appendOther(buffer, index, isTransparen); // 设置 尺寸 1x1
    index = this._appendGlobalColorTable(buffer, index, colorArray); // 设置 颜色
    if (isTransparen) {
      // index =     appendGraphicsControlExtension(buffer, index);
    }
    index = this._appendImageDescriptor(buffer, index); // 图像数据
    index = this._appendImageData(buffer, index); // 图像数据
    index = this._appendEnd(buffer, index); // 设置 end
    let finalBuffer = buffer.slice(0, index);
    return finalBuffer;
  }

  // Header
  _appendHeader(buffer, index) {
    let start = index;
    buffer[start++] = 0x47;
    buffer[start++] = 0x49;
    buffer[start++] = 0x46;
    buffer[start++] = 0x38;
    buffer[start++] = 0x39;
    buffer[start++] = 0x61;
    return start; // 6
  }

  // Logical Screen Descriptor
  _appendOther(buffer, index, isTransparen) {
    let start = index;
    buffer[start++] = 0x01;
    buffer[start++] = 0x00;
    buffer[start++] = 0x01;
    buffer[start++] = 0x00;

    buffer[start++] = isTransparen ? 0x00 : 0x80; // 透明不需要颜色
    buffer[start++] = 0x00; // 背景色索引
    buffer[start++] = 0x00; // Pixel Aspect Ratio

    return start; // 13
  }
  // 添加颜色
  _appendGlobalColorTable(buffer, index, colorArray) {
    let start = index;
    if (colorArray) {
      for (let color of colorArray) {
        buffer[start++] = color;
      }
      buffer[start++] = 0x00;
      buffer[start++] = 0x00;
      buffer[start++] = 0x00;
    }
    return start;
  }

  // 添加图像控制信息（静态图不用添加）
  _appendGraphicsControlExtension(buffer, index) {
    let start = index;
    buffer[start++] = 0x21;

    buffer[start++] = 0xf9;
    buffer[start++] = 0x04;

    buffer[start++] = 0x01;
    buffer[start++] = 0x0a;

    buffer[start++] = 0x00;
    buffer[start++] = 0x01;

    buffer[start++] = 0x00;

    return start;
  }

  // 添加图像描述符
  _appendImageDescriptor(buffer, index) {
    let start = index;
    buffer[start++] = 0x2c;

    buffer[start++] = 0x00;
    buffer[start++] = 0x00;

    buffer[start++] = 0x00;
    buffer[start++] = 0x00;

    buffer[start++] = 0x01;
    buffer[start++] = 0x00;

    buffer[start++] = 0x01;
    buffer[start++] = 0x00;

    buffer[start++] = 0x00;

    return start;
  }

  // 添加图像数据
  _appendImageData(buffer, index, isTransparen) {
    let start = index;
    buffer[start++] = 0x02;
    // 开始图像块
    buffer[start++] = 0x02; // 数据块长度

    buffer[start++] = isTransparen ? 0x4c : 0x44;

    buffer[start++] = 0x01;
    buffer[start++] = 0x00;
    // 本块结束

    return start;
  }

  _appendEnd(buffer, index) {
    let start = index;
    buffer[start++] = 0x3b;
    return start;
  }
}

module.exports = {
    OnePxGIF
}